//
//  QCHomeViewController.swift
//  爱汽车
//
//  Created by liujianlong on 2023/8/8.
//

import UIKit
import WebKit

class QCHomeViewController: QCViewController ,WKUIDelegate, WKNavigationDelegate,UIWebViewDelegate{
    var webView:WKWebView?
    var url:String = "http://www.airmaster-performance.com/"
    lazy private var progressView: UIProgressView = {
        self.progressView = UIProgressView.init(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.width, height: 1.0))
        self.progressView.tintColor = "#00E0D3".color()     // 进度条颜色
        self.progressView.trackTintColor = UIColor.white // 进度条背景色
        return self.progressView
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        clearWebCache()
        setUpWKwebView()
    }
    
    // MARK: - 清空缓存
    func clearWebCache() {
        let dateFrom: NSDate = NSDate.init(timeIntervalSince1970: 0)
        if #available(iOS 9.0, *) {
            let websiteDataTypes = WKWebsiteDataStore.allWebsiteDataTypes()
            WKWebsiteDataStore.default().removeData(ofTypes: websiteDataTypes , modifiedSince: dateFrom as Date) {
                print("清空缓存完成")
            }
        } else {
            let libraryPath = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true)[0]
            let cookiesFolderPath = libraryPath.appending("/Cookies")
            try? FileManager.default.removeItem(atPath: cookiesFolderPath)
        }
    }
    
    func setUpWKwebView() {
        let webview = WKWebView(frame: CGRect.init(x: 0, y: 0, width: kUIScreenWidth, height: kUIScreenHeight - kUINavigationBarHeight))
        let url = NSURL(string: self.url)
        let request = NSURLRequest.init(url: url! as URL)
        webview.load(request as URLRequest)
        self.view.addSubview(webview)
        self.view.addSubview(progressView)
        self.webView = webview
        self.webView?.uiDelegate = self
        self.webView?.navigationDelegate = self
        
        self.webView?.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil)
    }
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        
        if keyPath == "estimatedProgress"{
            progressView.alpha = 1.0
            progressView.setProgress(Float((self.webView?.estimatedProgress) ?? 0), animated: true)
            if (self.webView?.estimatedProgress ?? 0.0)  >= 1.0 {
                UIView.animate(withDuration: 0.3, delay: 0.1, options: .curveEaseOut, animations: {
                    self.progressView.alpha = 0
                }, completion: { (finish) in
                    self.progressView.setProgress(0.0, animated: false)
                })
            }
        }
    }
    
    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!){
        
    }
    func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
        print("")
    }
    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        print("")
    }
    func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!){
        
    }
    
    func webViewDidClose(_ webView: WKWebView) {
        
    }
    
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        
    }
    
    deinit {
        self.webView?.removeObserver(self, forKeyPath: "estimatedProgress")
        self.webView?.uiDelegate = nil
        self.webView?.navigationDelegate = nil
    }
    
}
